import 'package:flutter/material.dart';
import 'package:lib_music/bean/MusicBean.dart';

import '../../../generated/l10n.dart';
import 'package:lib_music/lib_music.dart';
import 'package:lib_music/lib_mic.dart';

import '../base/MyGestureDetector.dart';

class PlayListDialog {
  static Future<MusicBean?> show(BuildContext context) {
    return showModalBottomSheet<MusicBean>(
      context: context,
      builder: (BuildContext context) {
        return const _MusicListWidget();
      },
    );
  }
}

class _MusicListWidget extends StatefulWidget {
  const _MusicListWidget({super.key});

  @override
  State<_MusicListWidget> createState() => _MusicListWidgetState();
}

class _MusicListWidgetState extends State<_MusicListWidget> {
  List<MusicBean> _playList = [];

  @override
  void initState() {
    super.initState();
    _playList = LibMusic.instance.playList;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(top: 15),
      decoration: const BoxDecoration(color: Colors.blueGrey, borderRadius: BorderRadius.all(Radius.circular(6))),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [Text(S.of(context).str_id_29, style: const TextStyle(color: Colors.black, fontSize: 16))],
          ),
          Expanded(child: MyHomePage(playList: _playList)),
        ],
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final List<MusicBean> playList;

  const MyHomePage({super.key, required this.playList});

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late List<MusicBean> _items;

  @override
  void initState() {
    _items = widget.playList;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ReorderableListView.builder(
      itemBuilder: (BuildContext context, int index) {
        MusicBean bleBean = _items[index];

        bool isPlay = false;
        var playBean = LibMusic.instance.getCurrentMusic();

        if (playBean != null) {
          isPlay = playBean.url == bleBean.url;
        }

        return MyGestureDetector(
          key: ValueKey(bleBean),
          onTap: () {
            if (!isPlay) {
              Navigator.of(context).pop(bleBean);
            }
          },
          behavior: HitTestBehavior.opaque,
          child: Container(
            margin: const EdgeInsets.only(top: 10, left: 10, right: 10),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(60),
              border: Border.all(color:  Colors.black, width: 1),
            ),
            padding: const EdgeInsets.only(left: 15, right: 10),
            height: 44,
            child: Column(
              children: [
                Expanded(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text("${index + 1}.${bleBean.title!}", style: const TextStyle(color: Colors.black, fontSize: 16)),
                      isPlay ? Icon(Icons.stacked_bar_chart_outlined) : const SizedBox(),
                    ],
                  ),
                ),
              ],
            ),
          ),
        );
      },
      itemCount: _items.length,
      onReorder: (int oldIndex, int newIndex) {
        setState(() {
          if (newIndex > oldIndex) {
            newIndex -= 1;
          }
          final item = _items.removeAt(oldIndex);
          _items.insert(newIndex, item);
        });
      },
    );
  }
}
